﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.esriSystem;
using BesAsm.Emgaats.Core;

namespace BesAsm.Emgaats.Workflow
{
  public partial class WorkflowUserControl : UserControl
  {
    private WorkflowExtension _workflowExtension;

    public WorkflowUserControl(object hook)
    {
      InitializeComponent();

      this.Hook = hook;

      try
      {
        _workflowExtension = WorkflowExtension.GetExtension();
        _workflowExtension.RebindRequired += new EventHandler(OnRebindRequired);

        emgaatsExtensionBaseBindingSource.DataSource = Mediator.RegisteredExtensions;
        emgaatsLayerBindingSource.DataSource = LayerLibrarian.Library;
        modelBindingSource.DataSource = Mediator.Model;

        Mediator.ModelLoaded += new EventHandler(Mediator_ModelLoaded);
      }
      catch (Exception ex)
      {
      }
    }

    void Mediator_ModelLoaded(object sender, EventArgs e)
    {
      modelBindingSource.DataSource = Mediator.Model;
    }


    /// <summary>
    /// Host object of the dockable window
    /// </summary>
    private object Hook
    {
      get;
      set;
    }

    /// <summary>
    /// Implementation class of the dockable window add-in. It is responsible for 
    /// creating and disposing the user interface class of the dockable window.
    /// </summary>
    public class AddinImpl : ESRI.ArcGIS.Desktop.AddIns.DockableWindow
    {
      private WorkflowUserControl m_windowUI;

      public AddinImpl()
      {
      }

      protected override IntPtr OnCreateChild()
      {
        m_windowUI = new WorkflowUserControl(this.Hook);
        return m_windowUI.Handle;
      }

      protected override void Dispose(bool disposing)
      {
        if (m_windowUI != null)
          m_windowUI.Dispose(disposing);

        base.Dispose(disposing);
      }

    }

    private void OnRebindRequired(object sender, EventArgs e)
    {
      emgaatsLayerBindingSource.DataSource = LayerLibrarian.Library;
    }

    private void btnShowExtension_Click(object sender, EventArgs e)
    {
      IEmgaatsExtension ext = (IEmgaatsExtension)emgaatsExtensionBaseBindingSource.Current;
      if (ext != null)
        ext.ActivateExtension();
    }

  }
}
